package com.wanpinghui.wphlibrary.model.product;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.wphlibrary.WPHLibrary;
import com.wanpinghui.wphlibrary.common.IWPHApi;
import com.wanpinghui.wphlibrary.model.order.bean.OrderItemBean;
import com.wanpinghui.wphlibrary.model.order.bean.SingleOrderItem;
import com.wanpinghui.wphlibrary.model.product.bean.AddCartParamsBean;
import com.wanpinghui.wphlibrary.model.product.bean.CartBean;
import com.wanpinghui.wphlibrary.model.product.bean.EditCartItemCountParams;
import com.wanpinghui.wphlibrary.model.product.bean.OrderCreateParams;
import com.wanpinghui.wphlibrary.model.product.bean.ProductBean;
import com.wanpinghui.wphlibrary.model.product.bean.ProductGroupBean;
import com.wanpinghui.wphlibrary.model.product.bean.ProductPageBean;
import com.yunshl.yunshllibrary.storage.ShareDataManager;
import com.yunshl.yunshllibrary.storage.SharedPreferencesKey;
import com.yunshl.yunshllibrary.utils.LogUtils;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit.Network;
import retrofit.ToKenUtil;
import retrofit.callback.YRequestCallback;
import retrofit.callback.YunShlAction;
import retrofit.callback.YunShlResult;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/22
 ***/

public class ProductServiceImp implements ProductService {
    private Gson gson;

    @Override
    public void getCartCount(boolean isFirst, YRequestCallback<Integer> callback) {
        int cartCount = (int) ShareDataManager.getInstance().getParaLong(SharedPreferencesKey.KEY_CART_COUNT);
        if (cartCount > 0 && !isFirst) {
            if (callback != null) {
                callback.onSuccess(cartCount);
            }
        }else {
            getCartCountFromServer(callback);
        }
    }

    @Override
    public void getProductTypes(final YRequestCallback<List<ProductGroupBean>> callback) {
        Network.getYSApi(IWPHApi.class)
                .getProductTypes()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<List<ProductGroupBean>>>() {
                    @Override
                    public void call(YunShlResult<List<ProductGroupBean>> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(0, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void getProductList(int groupId, int supplierId, int userId, int currentPage, final YRequestCallback<ProductPageBean> callback) {
        Network.getYSApi(IWPHApi.class).getProductList(groupId,null,supplierId==0?null:supplierId,userId==0?null:userId,null,currentPage,20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<ProductPageBean>>() {
                    @Override
                    public void call(YunShlResult<ProductPageBean> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(0, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void addToCart(List<AddCartParamsBean> paramsList, final YRequestCallback callback) {
        Network.getYSApi(IWPHApi.class)
                .addProductToCart(paramsList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                            getCartCountFromServer(null);
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void addBuyerCart(List<AddCartParamsBean> paramsList, final YRequestCallback callback) {
        Network.getYSApi(IWPHApi.class)
                .addBuyerCartItems(paramsList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                            getCartCountFromServer(null);
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void updateCart(List<AddCartParamsBean> paramsList, final YRequestCallback callback) {
        Network.getYSApi(IWPHApi.class)
                .updateCart(paramsList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                            getCartCountFromServer(null);
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void getCartList(final YRequestCallback<List<CartBean>> callback) {
        Network.getYSApi(IWPHApi.class)
                .getCartInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<List<CartBean>>>() {
                    @Override
                    public void call(YunShlResult<List<CartBean>> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status , result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void changeCartItemCount(EditCartItemCountParams params, final YRequestCallback callback) {
        Network.getYSApi(IWPHApi.class)
                .updateCartItem(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<Object>>() {
                    @Override
                    public void call(YunShlResult<Object> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                            getCartCountFromServer(null);
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    @Override
    public void createOrder(OrderCreateParams params, final YRequestCallback<SingleOrderItem> callback) {
        Network.getYSApi(IWPHApi.class)
                .createOrder(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<SingleOrderItem>>() {
                    @Override
                    public void call(YunShlResult<SingleOrderItem> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                            getCartCountFromServer(null);
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status, result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    private void getCartCountFromServer(final YRequestCallback<Integer> callback) {
        if (TextUtil.isNotEmpty(ToKenUtil.getToken())) {
            Network.getYSApi(IWPHApi.class).getCartItemProductCount()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<YunShlResult<Integer>>() {
                        @Override
                        public void call(YunShlResult<Integer> result) {
                            if (result.isSuccess()) {
                                if (result.body != null) {
                                    WPHLibrary.getLibrary().onCartChange(result.body);
                                    ShareDataManager.getInstance().save(WPHLibrary.getLibrary().getContext(), SharedPreferencesKey.KEY_CART_COUNT, (long) result.body);
                                    if (callback != null) {
                                        callback.onSuccess(result.body);
                                    }
                                } else {
                                    WPHLibrary.getLibrary().onCartChange(0);
                                    ShareDataManager.getInstance().save(WPHLibrary.getLibrary().getContext(), SharedPreferencesKey.KEY_CART_COUNT, 0L);
                                    if (callback != null) {
                                        callback.onSuccess(0);
                                    }
                                }
                            } else {
                                if (callback != null) {
                                    callback.onSuccess(0);
                                }
                            }
                        }
                    }, new YunShlAction<Throwable>(callback));
        }
    }
}
